#include "../../common/detail.hpp"
#include "../../client/rpc_client.hpp"

using tmh_rpc::client::RpcCaller;

void callback(const Json::Value &res)
{
    ILOG("callback result:%d", res.asInt());
}
int main()
{
    // auto requestor=std::make_shared<tmh_rpc::client::Requestor>();
    // auto caller=std::make_shared<tmh_rpc::client::RpcCaller>(requestor);

    // //创建Dispatcher对象
    // auto dispatcher=std::make_shared<tmh_rpc::Dispatcher>();
    // auto rsp_cb=std::bind(&tmh_rpc::client::Requestor::onResponse,requestor.get(),\
    //             std::placeholders::_1,std::placeholders::_2);

    // dispatcher->registerHandler<tmh_rpc::BaseMessage>(tmh_rpc::MType::RSP_RPC,rsp_cb);//注册消息类型和回调的映射关系

    // // 创建Muduoclient对象,并且指定端口9090
    // auto client = tmh_rpc::ClientFactory::create("127.0.0.1", 9090);

    // //此时因为上面的映射,onMessage其实就是RpcRequest;此处通过bind将onMessage中(查找并调用)的方法传递给客户端的setMessage
    // auto message_cb=std::bind(&tmh_rpc::Dispatcher::onMessage,dispatcher.get(),std::placeholders::_1,std::placeholders::_2);
    // client->setMessageCallback(message_cb);

    // // 连接服务器
    // client->connect();

    // auto conn=client->connection();
    tmh_rpc::client::RpcClient client(false, "127.0.0.1", 8080);
    Json::Value params, result;
    params["num1"] = 10;
    params["num2"] = 40;
    ILOG("num1=%d, num2=%d", params["num1"].asInt(), params["num2"].asInt());
    bool ret = client.call("Add", params, result);
    if (ret != false)
    {
        ILOG("Syn result:%d", result.asInt());
    }

    params["num1"] = 1;
    params["num2"] = 4;
    // 异步调用
    tmh_rpc::client::RpcCaller::JsonAsyncResponse res_future;
    ret = client.call("Add", params, res_future);
    if (ret != false)
    {
        result = res_future.get();
        ILOG("Asyn result:%d", result.asInt());
    }

    params["num1"] = -1;
    params["num2"] = -10;
    ret = client.call("Add", params, callback);
    DLOG("---------------------------\n");
    sleep(3);
    return 0;
}
